﻿using System;
using System.Text;
using System.IO;
using System.Drawing;
using System.Collections;

using iTextSharp.text.pdf;

namespace iPdf
{
    class AttachmentExtractor : SimpleActor<object>
    {
        #region Fields
        protected string directory;
        #endregion

        protected override void Prepare()
        {
            directory = Helper.ShowChooseDirectoryDialog();
            if (string.IsNullOrEmpty(directory))
            {
                throw new ActionCancelled();
            }
        }

        protected override void Manipulate()
        {
            DateTime timeStamp = DateTime.Now;
            string date = timeStamp.ToLongDateString();
            string time = timeStamp.ToLongTimeString();
            string fileName = Path.GetFileNameWithoutExtension(workItem.Source);
            string storeDirectory = Path.Combine( directory, "ExtractedAttachmentsFrom" + fileName+ "On" +
                                                date.Replace(Helper.Slash, Helper.Dot) + "At" +
                                                time.Replace(Helper.Colon, Helper.Dot));

            PdfDictionary catalog = reader.Catalog;
            PdfDictionary names = (PdfDictionary)PdfReader
                    .GetPdfObject(catalog.Get(PdfName.NAMES));
            if (names != null)
            {
                PdfDictionary embFiles = (PdfDictionary)PdfReader.GetPdfObject(names.Get(new PdfName("EmbeddedFiles")));
                if (embFiles != null)
                {
                    Hashtable embMap = PdfNameTree.ReadTree(embFiles);
                    foreach(PdfObject value in embMap.Values)
                    {
                        try
                        {
                            PdfDictionary fileSpec = (PdfDictionary)PdfReader.GetPdfObject(value);
                            SharpHelper.UnpackFile(reader, fileSpec, storeDirectory);
                        }
                        catch { }
                    }
                }
            }
            for (int k = 1; k <= reader.NumberOfPages; ++k)
            {
                PdfArray annots = (PdfArray)PdfReader.GetPdfObject(reader
                        .GetPageN(k).Get(PdfName.ANNOTS));
                if (annots == null)
                    continue;
                for (int i = 0; i < annots.Length; ++i )
                {
                    try
                    {
                        PdfDictionary annot = annots.GetAsDict(i);
                        if (annot == null)
                        {
                            continue;
                        }

                        PdfName subType = (PdfName)PdfReader.GetPdfObject(annot
                                .Get(PdfName.SUBTYPE));
                        if (!PdfName.FILEATTACHMENT.Equals(subType))
                            continue;
                        PdfDictionary fileSpec = (PdfDictionary)PdfReader
                                .GetPdfObject(annot.Get(PdfName.FS));
                        SharpHelper.UnpackFile(reader, fileSpec, storeDirectory);
                    }
                    catch
                    {
                    }
                }
            }

            if (Directory.Exists(storeDirectory))
            {
                Helper.OpenDirectory(storeDirectory);
            }
        }
    }
}
